/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.Writer;
import java.sql.Clob;
import java.util.List;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;
import javacommon.util.DateTools;
import javacommon.util.Notes;
import oracle.sql.CLOB;

import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.lob.SerializableClob;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.util.ObjectUtils;

import com.tech15.core.dao.SysUserDao;
import com.tech15.core.model.Attach;
import com.tech15.core.model.SysUser;
import com.tech15.core.service.AttachManager;
import com.tech15.pub.dao.PubDataDao;
import com.tech15.pub.dao.PubInfoAuthDao;
import com.tech15.pub.dao.PubInfoDao;
import com.tech15.pub.model.PubData;
import com.tech15.pub.model.PubInfo;
import com.tech15.pub.model.PubInfoAuth;
import com.tech15.pub.vo.query.PubInfoQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class PubInfoManager extends BaseManager<PubInfo,java.lang.String>{

	private PubInfoDao pubInfoDao;
	private AttachManager attachManager;
	private PubDataDao pubDataDao;
	private SysUserDao sysUserDao;
	private PubInfoAuthDao pubInfoAuthDao;
	
	public void setPubInfoAuthDao(PubInfoAuthDao pubInfoAuthDao) {
		this.pubInfoAuthDao = pubInfoAuthDao;
	}

	public void setSysUserDao(SysUserDao sysUserDao) {
		this.sysUserDao = sysUserDao;
	}
	
	public void setPubDataDao(PubDataDao pubDataDao) {
		this.pubDataDao = pubDataDao;
	}
	public void setAttachManager(AttachManager attachManager) {
		this.attachManager = attachManager;
	}
	public void setPubInfoDao(PubInfoDao dao) {
		this.pubInfoDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.pubInfoDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(PubInfoQuery query) {
		return pubInfoDao.findPage(query);
	}
	
	@Transactional(readOnly=true)
	public Page findPageGzzd(PubInfoQuery query) {
		return pubInfoDao.findPageGzzd(query);
	}
	
	public PubInfo getById(String id){
		PubInfo pubInfo = pubInfoDao.getById(id);
		Clob clob = pubInfo.getNr();
		try {
			BufferedReader a = new BufferedReader(clob.getCharacterStream()); //以字符流的方式读入BufferedReader 
			String str = "";
			String content = "";
			while ((str = a.readLine()) != null) {
				content = content.concat(str);
			}
			pubInfo.setContent(content);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(ObjectUtils.isNotEmpty(pubInfo.getZt())){
			PubData pd = pubDataDao.getById(pubInfo.getZt());
			if(pd != null){
				pubInfo.setDataname(pd.getName());
			}
		}
		if(ObjectUtils.isNotEmpty(pubInfo.getLb())){
			PubData pd = pubDataDao.getById(pubInfo.getLb());
			if(pd != null){
				pubInfo.setCategoryName(pd.getName());
			}
		}
		if(ObjectUtils.isNotEmpty(pubInfo.getFbr())){
			SysUser user = sysUserDao.getById(pubInfo.getFbr());
			if(user != null){
			pubInfo.setRealname(user.getRealname());
			}
		}
		List<Attach> attach = attachManager.findByTableNameAndId(Notes.TABLE_PUB_INFO, ""+id);
		pubInfo.setAttach(attach);
		
		List<PubInfoAuth> poalist = pubInfoAuthDao.searchById(id);
		pubInfo.setUsers(poalist);
		
		return pubInfo;
	}
	public void saveInfo(PubInfo pubInfo, String realpath, List<File> attachFile, List<String> attachFileFileName, List<String> attachFileContentType) {
		if(pubInfo.getUserids() != null){
			pubInfo.setIsauth(1);
		}else{
			pubInfo.setIsauth(0);
		}
		pubInfo.setFbsj(DateTools.getCurrentTime());
		pubInfo.setNr(Hibernate.createClob(" "));
		pubInfoDao.save(pubInfo);
		pubInfoDao.flush();
		if(attachFile != null && attachFileFileName != null){
			attachManager.saveAttachs(realpath, pubInfo.getFbr(), Notes.TABLE_PUB_INFO, ""+pubInfo.getId(), attachFile, attachFileFileName, attachFileContentType);
		}
		try {
			pubInfoDao.refresh(pubInfo, LockMode.UPGRADE);
			SerializableClob cb = (SerializableClob) pubInfo.getNr();
			CLOB clob = (CLOB) cb.getWrappedClob();
			Writer writer = clob.getCharacterOutputStream();
			writer.write(pubInfo.getContent());
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		/*if(pubInfo.getUserids() != null){
			for(Long userid : pubInfo.getUserids()){
				PubInfoAuth poa = new PubInfoAuth();
				poa.setUserid(userid);
				poa.setPoid(pubInfo.getId());
				pubInfoAuthDao.save(poa);
			}
		}*/
	}
	
	public void updateInfo(PubInfo pubInfo, String realpath, List<File> attachFile, List<String> attachFileFileName, List<String> attachFileContentType) {
		if(pubInfo.getUserids() != null){
			pubInfo.setIsauth(1);
		}else{
			pubInfo.setIsauth(0);
		}
		//pubInfo.setFbsj(DateTools.getCurrentTime());
		pubInfo.setNr(Hibernate.createClob(" "));
		pubInfoDao.update(pubInfo);
		pubInfoDao.flush();
		/*if(attachFile != null){
			attachManager.delete(Notes.TABLE_PUB_INFO, pubInfo.getId());
			attachManager.saveAttachs(realpath, pubInfo.getFbr(), Notes.TABLE_PUB_INFO, ""+pubInfo.getId(), attachFile, attachFileFileName, attachFileContentType);
		}*/
		if(!ObjectUtils.isNullOrEmptyString(pubInfo.getUploadedAttachName())){
			String attachids = pubInfo.getUploadedAttachName().replaceAll(",", "','");
			attachids = attachids.replaceAll(" ", "");
			attachids = "'"+attachids+"'";
			attachManager.delete("delete attach t where t.tablename='"+Notes.TABLE_PUB_INFO+"' and t.tableid='"+pubInfo.getId()+"' and t.attachid not in ("+attachids+")");
		}
		if(attachFile != null && attachFileFileName != null){
			attachManager.saveAttachs(realpath, pubInfo.getFbr(), Notes.TABLE_PUB_INFO, ""+pubInfo.getId(), attachFile, attachFileFileName, attachFileContentType);
		}
		try {
			pubInfoDao.refresh(pubInfo, LockMode.UPGRADE);
			SerializableClob cb = (SerializableClob) pubInfo.getNr();
			CLOB clob = (CLOB) cb.getWrappedClob();
			Writer writer = clob.getCharacterOutputStream();
			writer.write(pubInfo.getContent());
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		/*if(pubInfo.getUserids() != null){
			pubInfoAuthDao.delete("delete pub_info_auth a where a.poid='"+pubInfo.getId()+"'");
			for(Long userid : pubInfo.getUserids()){
				PubInfoAuth poa = new PubInfoAuth();
				poa.setUserid(userid);
				poa.setPoid(pubInfo.getId());
				pubInfoAuthDao.save(poa);
			}
		}*/
	}
	
}
